www.gusucode.com > Java 编写画图工具源代码 > Java 编写画图工具源代码\www.gusucode.com\步骤8.txt
//使工具栏放置在左边 import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.event.*; import java.awt.geom.*; public class MyPaint extends JFrame { /** * 实现画图的基本功能 * @author Administrator * */ class DrawingPanel extends JPanel{ //构造DrawingPanel实例 public DrawingPanel(){ setBackground(Color.white); addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseListener()); } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D)g; g2d.setStroke(new BasicStroke(penSize)); g2d.setColor(color); if (flag == 0){ g2d.drawLine(oldX, oldY, newX, newY); }else if (flag == 1){ Rectangle2D rect = new Rectangle2D.Double((double)oldX, (double)oldY, (double)(newX - oldX), (double)(newY - oldY)); g2d.draw(rect); }else if (flag == 2){ Ellipse2D ellipse = new Ellipse2D.Double((double)oldX, (double)oldY, (double)(newX - oldX), (double)(newY - oldY)); g2d.draw(ellipse); }else if (flag == 3){ Line2D line2 = new Line2D.Double((double)oldX, (double)oldY, (double)(newX), (double)(newY)); g2d.draw(line2); } } class MyMouseListener extends MouseAdapter implements MouseMotionListener{ //单击响应记录座标 public void mousePressed(MouseEvent e){ oldX = newX = e.getX(); oldY = newY = e.getY(); prompt.setText("按住不放拖动可以画出线条"); } //拖动响应画出线条 public void mouseDragged(MouseEvent e){ if (flag == 0){ oldX = newX; oldY = newY; newX = e.getX(); newY = e.getY(); prompt.setText("松开左键可以释放画笔"); repaint(); }else{ newX = e.getX(); newY = e.getY(); } } public void mouseReleased(MouseEvent e){ prompt.setText("按住不放拖动可以画出线条"); newX = e.getX(); newY = e.getY(); repaint(); } } } /** * 添加菜单 * @param title */ public JMenuBar createMenubar(){ //菜单条 JMenuBar menubar = new JMenuBar(); //菜单项目 JMenu fileMenu, drawMenu, helpMenu; /* 文件菜单 */ fileMenu = new JMenu("文件(F)"); //打开文件 JMenuItem menuitem = new JMenuItem("打开(O)", new ImageIcon("openfile.gif")); fileMenu.add(menuitem); //建立新文件 menuitem = new JMenuItem("新建(N)", new ImageIcon("newfile.gif")); fileMenu.add(menuitem); //程序保存 menuitem = new JMenuItem("保存(S)", new ImageIcon("savefile.gif")); fileMenu.add(menuitem); //程序退出 fileMenu.addSeparator(); menuitem = new JMenuItem("退出(X)", new ImageIcon("quit.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ System.exit(0); } } ); fileMenu.add(menuitem); menubar.add(fileMenu); /* 绘图菜单 */ drawMenu = new JMenu("绘制(D)"); //颜色 menuitem = new JMenuItem("颜色选取(C)", new ImageIcon("color.gif")); menuitem.addActionListener(new ButtonListener()); drawMenu.add(menuitem); drawMenu.addSeparator(); //粗细 menuitem = new JMenuItem("线条大小(L)", new ImageIcon("change.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String inputValue = JOptionPane.showInputDialog("请输入线条的大小 (0.0 - 5.0)", "1.0"); penSize = Float.parseFloat(inputValue); } } ); drawMenu.add(menuitem); drawMenu.addSeparator(); JMenu draw = new JMenu("给制图形(D)"); drawMenu.add(draw); menuitem = new JMenuItem("矩形绘制(R)", new ImageIcon("Rectangle.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 1; } }); draw.add(menuitem); menuitem = new JMenuItem("圆形绘制(C)", new ImageIcon("round.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 2; } }); draw.add(menuitem); menuitem = new JMenuItem("线条绘制(L)", new ImageIcon("line.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 3; } }); draw.add(menuitem); draw.addSeparator(); menuitem = new JMenuItem("画笔操作(P)", new ImageIcon("pen.gif")); menuitem.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 0; } }); draw.add(menuitem); menubar.add(drawMenu); /* 帮助菜单 */ helpMenu = new JMenu("帮助(H)"); menuitem = new JMenuItem("关于 绘图工具(A)"); menuitem.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ String str = new String("java程序设计实验二 绘图工具\n设计人:戴浩彬\n完成时间:2008-12-9"); JOptionPane.showMessageDialog(null, str); } }); helpMenu.add(menuitem); menubar.add(helpMenu); return menubar; } //响应颜色选取菜单的事件 class ButtonListener implements ActionListener{ public void actionPerformed(ActionEvent e){ dialog.setVisible(true); color = colorChooser.getColor(); } } /** * 构造函数,初始化 * @param title */ public MyPaint(String title){ super(title); colorChooser = new JColorChooser(); dialog = JColorChooser.createDialog(container, "Color Chooser", false, colorChooser, new ButtonListener(), new ButtonListener()); toolbar = createToolBar(); } //内容面板 Container container = getContentPane(); //座标的记录 int oldX, oldY, newX, newY; //画图面板 DrawingPanel panel = new DrawingPanel(); //画笔颜色 Color color = Color.blue; //程序的提示信息 JLabel prompt = new JLabel("欢迎使用本绘图工具绘制图形", new ImageIcon("message0.gif"), JLabel.LEFT); //颜色选取器 JColorChooser colorChooser = null; //颜色选取的对话框 JDialog dialog = null; //画笔的大小 float penSize = 1.0f; //画笔的状态 int flag = 0; //工具栏 JToolBar toolbar; public JToolBar createToolBar(){ JToolBar toolBar = new JToolBar(1); JButton button = new JButton(new ImageIcon("newfile.gif")); button.setToolTipText("新建文件"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ } }); toolBar.add(button); button = new JButton(new ImageIcon("openfile.gif")); button.setToolTipText("打开文件"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ } }); toolBar.add(button); button = new JButton(new ImageIcon("save.gif")); button.setToolTipText("保存文件"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ } }); toolBar.add(button); toolBar.addSeparator(); button = new JButton(new ImageIcon("pen.gif")); button.setToolTipText("使用画笔绘图"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 0; } }); toolBar.add(button); button = new JButton(new ImageIcon("color.gif")); button.setToolTipText("选择颜色"); button.addActionListener(new ButtonListener()); toolBar.add(button); button = new JButton(new ImageIcon("change.gif")); button.setToolTipText("改变画笔大小"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String inputValue = JOptionPane.showInputDialog("请输入线条的大小 (0.0 - 5.0)", "1.0"); penSize = Float.parseFloat(inputValue); } }); toolBar.add(button); toolBar.addSeparator(); button = new JButton(new ImageIcon("round.gif")); button.setToolTipText("画圆形"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 2; } }); toolBar.add(button); button = new JButton(new ImageIcon("Rectangle.gif")); button.setToolTipText("画矩形"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 1; } }); toolBar.add(button); button = new JButton(new ImageIcon("line.gif")); button.setToolTipText("画直线"); button.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ flag = 3; } }); toolBar.add(button); return toolBar; } //生成JFrame public void go(){ setSize(500, 500); setLocation(200, 200); setJMenuBar(createMenubar()); toolbar.setMargin(new Insets(5, 5, 5, 5)); container.add(toolbar, BorderLayout.WEST); container.add(panel, BorderLayout.CENTER); //设置标签颜色 prompt.setForeground(Color.blue); container.add(prompt, BorderLayout.SOUTH); setVisible(true); addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } public static void main(String[] args) { //设定系统外观为默认外观 try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e){ System.out.print(e); } //生成主要的框架 MyPaint paint = new MyPaint("java实验 绘图工具 V2.0"); paint.go(); } }